<template>
	<view>
		<view class="header">
			<image src="@/pages7/static/images/back.jpg" class="header-nav" mode="widthFix"></image>
			<view class="header-back" @tap="back">
				<image src="@/static/images/leftfff.png" class="back" mode="widthFix"></image>
			</view>
			<view class="header-search">
				<view class="input">
					<image src="@/static/images/sousuo1.png" class="search-icon" mode="widthFix"></image>
					<input class="search-text" type="text" placeholder="请输入关键字" v-model="keyword">
					<view class="search-btn" @tap="getSearchRes">搜索</view>
				</view>
			</view>
		</view>
		<!--  -->
		<view style="height: 83rpx;"></view>
		<view class="center">
			<view class="center-item">

				<view class="" @tap="rentHouse(1)">
					<image src="@/pages7/static/images/house_sale.png" class="item-image" mode="widthFix"></image>
					<view class="" style="font-size: 30rpx;">现房信息</view>
				</view>

				<view class="" @tap="rentHouse(2)">
					<image src="@/pages7/static/images/house_rent.png" class="item-image" mode="widthFix"></image>
					<view class="" style="font-size: 30rpx;">快捷酒店</view>
				</view>

				<view class="" @tap="goExchangeCenter">
					<image src="@/pages7/static/images/huhuangzhongxin.png" class="item-image" mode="widthFix"></image>
					<view class="" style="font-size: 30rpx;">互换中心</view>
				</view>

				<view class="" @tap="goCertificateCenter">
					<image src="@/static/images/jigongdaiban.png" class="item-image" mode="widthFix"></image>
					<view class="" style="font-size: 30rpx;">智慧培训</view>
				</view>

				<view class="" @tap="buyInsurance">
					<image src="@/static/images/yiwai.png" class="item-image" mode="widthFix"></image>
					<view class="" style="font-size: 30rpx;">保险</view>
				</view>
			</view>
		</view>
		<!--  -->
		<view style="height: 50rpx;"></view>
		<view class="footer">
			<view class="footer-title">
				<view class="footer-item">
					<view @click="chenked(0)" :class="currentIndex==0?'select':''">精选</view>
					<view class="" :class="currentIndex==0?'footer-image':''"></view>
				</view>
				<!-- <view class="footer-item">
					<view @click="chenked(1)" :class="currentIndex==1?'select':''">现房信息</view>
					<view class="" :class="currentIndex==1?'footer-image':''"></view>
				</view>
				<view class="footer-item">
					<view @click="chenked(2)" :class="currentIndex==2?'select':''">住宿</view>
					<view class="" :class="currentIndex==2?'footer-image':''"></view>
				</view> -->
			</view>
			<view style="height: 30rpx;"></view>
			<view class="item-data">
				<view class="data" @tap="detail(item.id)" v-for="(item,index) in lists" :key="index">
					<image :src="item.pic" class="data-image" mode="widthFix"></image>
					<view class="location">
						<image src="@/static/images/weizhifff.png" class="location-img" mode="widthFix"></image>
						<view class="location-text">{{item.district}}</view>
					</view>
					<view class="" style="padding: 10rpx;">
						<view class="data-title">{{item.title}}</view>
						<view class="solid">
							<view class="solid-data" v-for="(iitem,iindex) in item.services">{{iitem}}</view>
						</view>
						<view class="price">
							<view class="price-item">{{item.expect_price}}</view>
							<view class="price-box">万元</view>
						</view>
					</view>
				</view>

			</view>
			<uni-load-more :status="loadingType"></uni-load-more>
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<view style="height: 150rpx;"></view>
		<view class="Btn" @click="toggle('bottom')">
			<view class="button" style="letter-spacing: 4upx;">发布</view>
		</view>
		<!-- #endif -->
		<uni-popup ref="popup" background-color="#fff">
			<!-- <uni-popup ref="popup" background-color="#fff" @change="change"> -->
			<view class=" popup-content">
				<view class="" @tap="lssueHouse">
					<image src="@/static/images/ninsu.png" class="item-image" mode="widthFix"></image>
					<view class="" style="font-size: 28rpx;">现房信息</view>
				</view>

				<!-- <view class="" @tap="lssueHouse2">
					<image src="@/static/images/zhusu.png" class="item-image" mode="widthFix"></image>
					<view class="" style="font-size: 28rpx;">住宿</view>
				</view> -->

				<view class="" @tap="goExchange">
					<image src="@/pages7/static/images/huhuangzhongxin.png" class="item-image" mode="widthFix"></image>
					<view class="" style="font-size: 28rpx;">互换中心</view>
				</view>

				<view class="" @tap="goCertificate">
					<image src="@/static/images/jigongdaiban.png" class="item-image" mode="widthFix"></image>
					<view class="" style="font-size: 28rpx;">智慧培训</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				keyword: '',
				currentIndex: 0,
				page: 1,
				loadingType: 'more',
				lists: []
			};
		},

		//下拉到底获取新的数据
		onReachBottom() {
			this.loadData();
		},

		onLoad: function() {

			uni.showLoading({
				title: '加载中...'
			});

			this.loadData();
			setTimeout(() => {
				this.isLoaded = true;
				uni.hideLoading();
			}, 600)

		},
		methods: {
			async loadData(type = 'add') {
				//这里是将订单挂载到tab列表下
				if ((this.loadingType === 'loading' || this.loadingType === 'noMore') && type == 'add') {
					//防止重复加载
					return;
				}

				this.loadingType = 'loading';

				let dataObj = {
					page: this.page,
					keyword: this.keyword
				}
				let url = '';
				switch (this.currentIndex) {
					case 0:
						url = '/houses_list';
						break;
					case 1:
						url = '/homestay_list';
						dataObj.homeType = 1;
						break;
					case 2:
						url = '/homestay_list';
						dataObj.homeType = 2;
						break;
				}

				let ldata = await this.$myRequest({
					url: url,
					data: dataObj,
					methods: 'post'
				})

				let ndata = ldata.data.data.list;
				if (ndata.length > 0) {
					this.lists = [...this.lists, ...ndata];
					let count = ldata.data.data.totalList;
					if (this.lists.length < count) {
						this.page = this.page + 1;
						this.loadingType = 'more';
					} else {
						this.loadingType = 'noMore';
					}
				} else {
					this.loadingType = 'noMore';
				}
			},

			getSearchRes() {
				this.lists = [];
				this.page = 1;
				this.loadingType = 'more';

				this.loadData('refresh')
			},

			lssueHouse() {
				uni.navigateTo({
					url: '/pages7/pages/nimble/lssueHouse'
				})
			},

			lssueHouse2() {
				uni.navigateTo({
					url: '/pages7/pages/nimble/lssueHouse2'
				})
			},

			goExchangeCenter() {
				uni.navigateTo({
					url: '/pages7/pages/nimble/exchange_center'
				})
			},

			goCertificateCenter() {
				uni.navigateTo({
					url: '/pages7/pages/nimble/certificate_center'
				})
			},

			goExchange() {
				uni.navigateTo({
					url: '/pages7/pages/nimble/exchange_publish'
				})
			},

			goCertificate() {
				uni.navigateTo({
					url: '/pages7/pages/nimble/certificate_publish'
				})
			},

			detail(id) {
				uni.navigateTo({
					url: '/pages7/pages/nimble/houseDetile?id=' + id
				})
			},

			back() {
				uni.navigateTo({
					url: '/pages/index/index'
				})
			},
			chenked(index) {
				this.currentIndex = index;
				this.page = 1;
				this.lists = [];
				this.loadData('refresh');
			},
			toggle(type) {
				this.type = type
				// open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
				this.$refs.popup.open(type)
			},
			rentHouse(type) {
				let url = '/pages7/pages/nimble/houseSale';
				if (type == 2) {
					url = '/pages7/pages/nimble/houseRent';
				}
				uni.navigateTo({
					url: url
				})
			},

			// 添加保险记录
			async buyInsurance() {
				let result = this.$api.isLogin();
				if (result === false) {
					return false;
				}

				let res = await this.$myRequest({
					url: '/add_insurance_order',
					data: {
						token: uni.getStorageSync('token')
					},
					methods: 'POST'
				})
				let data = res.data;
				// console.log(data)
				switch (data.status) {
					case 2:
						// this.jobData[index].is_insurance = true;
						this.$api.msg(data.msg)
						break;
					case 1:
						let link = res.data.linkUrl
						// #ifdef H5
						if (this.$wechat && this.$wechat.isWechat()) {
							window.location.href = link
						} else {
							uni.navigateTo({
								url: '/pages6/pages/insurance/insurance?url=' + link
							})
						}
						// #endif

						// #ifndef H5
						uni.navigateTo({
							url: '/pages6/pages/insurance/insurance?url=' + link
						})
						// #endif
						break;
				}
			}
		},
	}
</script>

<style lang="scss">
	.Btn {
		position: fixed;
		bottom: 0;
		width: 100%;
		// height: 150rpx;
		padding: 20rpx 0;
		background-color: #ffffff;
		text-align: center;

		.button {
			background-color: #f3511a;
			color: white;
			height: 90rpx;
			line-height: 90rpx;
			width: 90%;
			margin: 0 auto;
			border-radius: 20rpx;
		}
	}

	.popup-content {
		height: 350rpx;
		text-align: center;
		display: grid;
		grid-template-columns: repeat(3, 250rpx);
		grid-gap: 30rpx 0;

		.item-image {
			width: 105rpx;
			height: 105rpx;
			margin-bottom: 10rpx;
		}

	}

	.header {
		width: 100%;
		position: relative;

		.header-nav {
			width: 100%;
			height: 284rpx;

		}

		.header-back {
			position: absolute;
			top: 120rpx;
			left: 20rpx;
		}

		.back {
			width: 17rpx;
			height: 30rpx;
		}

		.header-search {
			position: absolute;
			top: 245rpx;
			left: 20rpx;

			.input {
				display: flex;
				background-color: white;
				width: 710rpx;
				height: 80rpx;
				padding: 10rpx;
				border: 3rpx solid #f3511a;
				border-radius: 100rpx;
				align-items: center;

				.search-text {
					flex: 1;
					color: #6d6e6e;
					font-size: 28rpx;
				}

				.search-btn {
					font-size: 30rpx;
					background-color: #f3511a;
					color: white;
					width: 110rpx;
					letter-spacing: 2rpx;
					height: 57rpx;
					line-height: 57rpx;
					text-align: center;
					border-radius: 50rpx;
				}

				.search-icon {
					width: 25rpx;
					height: 25rpx;
					margin: 0 10rpx;
				}
			}

		}

	}

	.center {
		.center-item {
			text-align: center;
			display: grid;
			grid-template-columns: repeat(4, 190rpx);
			// grid-template-rows: repeat(2, 160rpx);
			grid-gap: 30rpx 0;

			.item-image {
				width: 105rpx;
				height: 105rpx;
				margin-bottom: 10rpx;
			}
		}
	}

	.footer {
		.footer-title {
			margin-left: 10rpx;
			display: flex;
		}

		.footer-item {
			margin: 0 20rpx;
			color: #6d6e6e;
			height: 50rpx;
			text-align: center;
			letter-spacing: 2rpx;
			font-size: 30rpx;
			// border-bottom: 4rpx solid #f24619;
		}

		.footer-image {
			height: 10rpx;
			width: 40rpx;
			margin: 5rpx auto;
			background-image: url('@/static/images/weinituijian.png');
			background-size: 40rpx;
		}

		.item-data {
			// text-align: center;
			display: grid;
			grid-template-columns: repeat(2, 340rpx);
			// grid-template-rows: repeat(2, 540rpx);
			grid-gap: 30rpx;
			margin-left: 24rpx;

			// position: relative;
			.location {
				position: absolute;
				// bottom: 10rpx;
				top: 310rpx;
				left: 20rpx;
				display: flex;
				align-items: center;
			}

			.location-text {
				margin-left: 10rpx;
				color: white;
				font-size: 26rpx;
				width: 88%;
				word-break: keep-all;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}

			.location-img {
				width: 23rpx;
				height: 26rpx;
			}

			.data {
				box-shadow: 5rpx 5rpx 15rpx #c4c4c7;
				border-radius: 20rpx;
				position: relative;

				.data-image {
					width: 340rpx;
					height: 360rpx !important;
					border-top-left-radius: 20upx;
					border-top-right-radius: 20upx;
				}

				.data-title {
					font-weight: 600;
					width: 300rpx;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
					margin-left: 10rpx;
					font-size: 30rpx;
					margin-bottom: 10rpx;
				}

				.solid {
					display: flex;
					width: 320rpx;
					white-space: nowrap;
					overflow: hidden;

					.solid-data {
						margin: 10rpx;
						background-color: #e1f0fe;
						color: #3b8bfc;
						padding: 10rpx;
						font-size: 24rpx;
						border-radius: 10rpx;
					}
				}

				.price {
					display: flex;
					margin-left: 10rpx;
					color: #f3511a;

					.price-item {
						font-size: 40rpx;
						margin-right: 5rpx;
					}

					.price-box {
						// color: #6d6e6e;
						font-size: 26rpx;
						align-self: center;
						margin-top: 10rpx;
					}
				}
			}

		}
	}

	.select {
		color: black;
	}
</style>
</style>